package com.chuangmei.com.arindoorlocationsystem.config.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.View;

import com.chuangmei.com.arindoorlocationsystem.R;
import com.chuangmei.com.arindoorlocationsystem.base.BaseFragment;
import com.chuangmei.com.arindoorlocationsystem.config.ConfigMessageActivity;
import com.chuangmei.com.arindoorlocationsystem.utils.LogUtils;

import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 项目名称：ARIndoorLocationSystem
 * 类描述：跳转关联外部App页面
 * 创建人：LuoHao
 * 创建时间：2018/4/24 9:35
 * 修改人：LuoHao
 * 修改时间：2018/4/24 9:35
 * 修改备注：
 */

public class JumpAppFragment extends BaseFragment {

    private PackageManager mPackageManager;
    private List<ResolveInfo> mAllApps;
    private Unbinder unbinder;

    /**
     * 检查系统应用程序，并打开
     */
    private void openApp() {
        //应用过滤条件
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mPackageManager = getContext().getPackageManager();
        mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
        //按包名排序
        Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));

        for (ResolveInfo res : mAllApps) {
            //该应用的包名和主Activity
            String pkg = res.activityInfo.packageName;
            String cls = res.activityInfo.name;
            //判断跳转的App是否存在
            if (pkg.contains("cn.xaninfo.forestryoa.planningdesign")) {
                ComponentName componet = new ComponentName(pkg, cls);

                Intent intent = new Intent();
                intent.setComponent(componet);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
                //打开另一个App后，本Fragment返回到上一个Fragment，解决被打开App返回本App后两个Fragment叠加问题。
                getActivity().getSupportFragmentManager().popBackStack();
                return;
            } else {

            }
        }
        //没有找到跳转的程序，可以做下载等逻辑处理
        LogUtils.showToastShort("XXX不存在,请下载或手动进入XXX进行业务操作");
    }

    @Override
    protected int setLayout() {
        return R.layout.fragment_jump_app;
    }

    @Override
    protected void initView(View view) {
        unbinder = ButterKnife.bind(this, view);
    }

    @OnClick(R.id.mBT_jump_app)
    public void onViewClicked() {
        openApp();
    }

    @Override
    public void onResume() {
        super.onResume();
        ((ConfigMessageActivity) getActivity()).chengTitle(getResources().getString(R.string.jump_title));
    }

    @Override
    public void onPause() {
        super.onPause();
        ((ConfigMessageActivity) getActivity()).chengTitle(getResources().getString(R.string.config_title));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
